<?php
class Solicitacao extends Zend_Db_Table 
{
	protected $_name = "solicitacao";
	private $_idSolic;
        
        
        public function setIdSolic($value)
        {
            $this->_idSolic = $value;
        }
        
        
        
	public function lista(array $request)
	{
		$dados = array();
		
                $select = $this->select()
			       ->from($this, 'COUNT(*) AS qtd')
			       ->where("situacao = ? ",'P');
                
		$result = $this->fetchRow($select);
                		
		if($result->qtd > 0){
			$total_pages = ceil($result->qtd / $request['rows']);
		}else{
			$total_pages = 0;
		}

		if($request['page'] == 1){
			$start = 0;
		}else{
			$start = ($request['rows'] * $request['page']) - $request['rows'];
		}

		$dados['total']	= $total_pages;
		$dados['page']	= $request['page'];
		$dados['records'] = $result->qtd;
		
		$select  = " SELECT s.idSolicitacao, ";
                $select .= " s.dataCadastro, c.NomeCurto as NomeCliente,";
                $select .= " CASE s.situacao WHEN 'A' THEN 'Novo'  WHEN 'P' THEN 'Parcial'";
                $select .= " ELSE 'Pendente' END SitSolicitacao";
            
                $select .= " FROM Solicitacao s";
                $select .= " inner join cliente c on (s.idCliente = c.idCliente)";
                $select .= " WHERE s.situacao in ('A','P')";
		
                if($request['sidx'])
		{
				$select .= " ORDER BY " . $request['sidx']  . " " . $request['sord'] . " LIMIT " . $start ." , ". $request['rows'] ;
		}
		else
		{
				$select .=  " LIMIT " . $start ." , ". $request['rows'] ;
			
		}
		 
		$solicitacao = $this->_db->fetchAll($select);
	 
		for($cont = 0; $cont < count($solicitacao); $cont++) {
			$dados['rows'][$cont]['id'] = $solicitacao[$cont]['idSolicitacao'];
			$dados['rows'][$cont]['cell'] = array(
								$solicitacao[$cont]['idSolicitacao'],
								$solicitacao[$cont]['dataCadastro'],
								$solicitacao[$cont]['NomeCliente'],
								$solicitacao[$cont]['SitSolicitacao']
					);
		}
		
		return $dados;
	}
	
	 
	
	
	
}